<?php

namespace app\admin\controller;

use \tp5er\Backup;

/**
 * 数据备份
 * User: tanhuaxin
 * Date: 2019/3/13
 * Time: 下午10:54
 */
class Databackup extends Controller
{

    /**
     * 数据备份列表
     */
    function lists()
    {
        if (request()->isAjax()) {
            $db = new Backup();
            $list['data'] =  $db->fileList();
            foreach ($list['data'] as $key=>$val){
                $list['data'][$key]['create_time'] = $key;
                $list['data'][$key]['size'] = getFilesize($val['size']);
            }
            return ajax_list($list);
        } else {
            return $this->fetch();
        }
    }

    /**
     * 数据备份
     */
    function backup()
    {
        $db = new Backup();
        $tables = $db->dataList(); //获取数据库所有表的信息
        foreach ($tables as $k => $v) {
            $db->backup($v['name'], 0); //循环所有表备份表和数据
        }
        $file = $db->getFile(); //获取所备份文件的文件名
        return $this->renderSuccess('操作成功');
    }

    /**
     * 数据下载
     */
    function download($time)
    {
        $db= new Backup();
        echo $db->downloadFile($time);
    }

    /**
     * 数据删除
     */
    function delete($time)
    {
        $db= new Backup();
        if($db->delFile($time)){
            return $this->renderSuccess('备份文件删除成功！');
        }else{
            return $this->renderError('备份文件删除失败，请检查权限！');
        }
    }
}
